<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class MilestonePost extends Milestone
{

    function __construct()
    {
        parent::__construct();

        $this->levelable = ( $this->id == $this->main_milestone ) && ($this->closed == 'N') && $this->iam_owner;
        $this->voteable = ( $this->closed == 'N' ); //aunque lo haya votado puede cambiar el voto
        $this->closeable = $this->iam_owner && ( $this->closed == 'N' );
        $this->deletable = $this->iam_owner;
        $this->enclosable = $this->iam_owner && ( $this->closed == 'N' );
    }

    function doPOST( Event $e )
    {

        if ( $e->tag == 'vote' ) {

            if ( $this->votable && in_array( $_POST[ 'voto' ],
                            array( '+1', '-1' ) ) ) {

                if ( !isset( $_SESSION[ 'votos' ][ $this->id ] ) ) {

                    if ( $_POST[ 'voto' ] == '-1' )
                            $sql = 'UPDATE milestone SET votes_plus=votes_plus-1 WHERE id=' . $this->id;
                    else
                            $sql = 'UPDATE milestone SET votes_plus=votes_plus+1 WHERE id=' . $this->id;
                    MyLink::$default->execute( $sql );

                    $_SESSION[ 'votos' ][ $this->id ] = $_POST[ 'voto' ];

                    $sql = 'INSERT INTO users_votes (milestone,user,vote) VALUES (' . $this->id . ',' . User::getLogged()->getId() . ',' . $_POST[ 'voto' ] . ')';
                    MyLink::$default->execute( $sql );
                }
                else {

                    if ( $_SESSION[ 'votos' ][ $this->id ] == '-1' ) {

                        if ( $_POST[ 'voto' ] == '+1' ) {
                            $sql = 'UPDATE milestone SET votes_plus=votes_plus-1, vote_minus=vote_minus+1 WHERE id=' . $this->id;
                            MyLink::$default->execute( $sql );
                        }
                    } else {

                        if ( $_POST[ 'voto' ] == '+1' ) {
                            $sql = 'UPDATE milestone SET votes_plus=votes_plus+1, vote_minus=vote_minus-1 WHERE id=' . $this->id;
                            MyLink::$default->execute( $sql );
                        }
                    }

                    $sql = 'UPDATE users_votes SET vote=' . $_POST[ 'voto' ] . ' WHERE milestone=' . $this->id . ' AND user=' . User::getLogged()->getId();
                    MyLink::$default->execute( $sql );
                }


                $_SESSION[ 'votos' ][ $this->id ] = $_POST[ 'voto' ];
            }

            $e->cancelBubble();
        } else if ( $e->tag == 'close' ) {

            if ( $this->closeable ) {

                //$sql='UPDATE milestones SET updated=NOW(), closed=\'S\' WHERE id='.$this->id;
                //MyLink::$default->execute($sql);
                //$sql='UPDATE milestones SET updated=NOW(), closed=\'S\' WHERE trace LIKE \'%.'.$this->id.'.%\'';
                //MyLink::$default->execute($sql);
                /*
                 * 
                 */
                $admins = array( );
                Node::getArray( 'user_p_admin', $admins );

                foreach ( $admins as $a ) {
                    $sql = 'INSERT INTO notifications ( milestone, user, type, who ) VALUES (' . $this->id . ',' . $a->id . ',\'CLOSE_REQUEST\',' . User::getLogged()->getId() . ')';
                    MyLink::$default->execute( $sql );
                }
            }
        } else if ( $e->tag == 'new_enclosed' ) {

            if ( $this->enclosable ) {

                if ( !empty( $_POST[ 'url' ] ) || !empty( $_FILES[ 'file' ] ) && file_exists( $_FILES[ 'file' ][ 'tmp_name' ] ) ) {
                    $type = 'LINK';

                    //clip-audio, sonido, clip-video, imagen, grafico, documento de texto, base de datos, hoja de calculo
                    //bien tengo que actualizar el numero de encloseds
                    $sql = 'INSERT INTO encloseds (project,type,active,title,url,revision,extension) VALUES (' . $this->project . ',' . $type . ',\'S\',' . MyLink::quote( $_POST[ 'title' ] ) . ',' . MyLink::quote( $_POST[ 'url' ] ) . ',0,\'' . $extension . '\')';
                    MyLink::$default->execute( $sql );

                    $id = MyLink::$default->insert_id;

                    if ( empty( $_FILES[ 'file' ] ) ) {
                        //si hay archivo tengo que ponerlo en la carpeta del proyecti
                        //antes tengo que determinar de que tipo es

                        if ( move_uploaded_file( $_FILES[ 'file' ][ 'tmp_name' ],
                                        'enclosed_files/' . $this->project . '/' . $id . '.0.' . $extension ) ) {
                            
                        }
                    }

                    $this->attach( $id );
                }
            }
        } else if ( $e->tag == 'attach_enclosed' ) {//se genera y lo procesa el enclosed comprobando en su nodo padre cual es el 
            $e->cancelBubble();
        } else if ( $e->tag == 'unattach_enclosed' ) {//se genera y lo procesa el enclosed comprobando en su nodo padre cual es el 
            $e->cancelBubble();
        } else if ( $e->tag == 'delete_enclosed' ) {
            $e->cancelBubble();
        } else if ( $e->tag == 'delete' ) {

            if ( $e->souce === $this ) {
                if ( $this->deletable ) {
                    //@TODO tengo que borrar todos las respuestas sucesivas
                    //SI ME INCORPORO A LA TRAZA


                    $sql = 'DELETE FROM milestones WHERE trace LIKE \'%.' . $this->id . '.%\'';
                    MyLink::$default->execute( $sql );


                    $this->notifyOwner( 'DELETE' );
                }
            }
            else $this->cancelBubble();
        }
        else if ( $e->tag == 'change_level' ) {
            if ( $this->leveleable ) {

                //$nivel=(int)$_POST['privacy'];//0: public, 1:enterprise, 2:private

                if ( $_POST[ 'privacy' ] == 'R_PUB_PRO' ) {
                    if ( $this->privacy_milestone != 'R_PUB_PRO' ) {
                        //lo cambio a publico
                        //si solo permito cambiar privacidad

                        $sql = 'UPDATE milestones SET privacy_milestone=\'R_PUB_PRO\' WHERE main_milestone=' . $this->id;
                        MyLink::$default->execute( $sql );

                        $sql = 'UPDATE milestones SET privacy_milestone=\'R_PUB_PRO\' WHERE id=' . $this->id;
                        MyLink::$default->execute( $sql );

                        $this->notifyOwner( 'PRIVACY' );
                    }
                } else if ( $_POST[ 'privacy' ] == 'R_ENT_PRO' ) {
                    if ( $this->privacy_milestone != 'R_ENT_PRO' ) {
                        //lo cambio a publico
                        $sql = 'UPDATE milestones SET privacy_milestone=\'R_ENT_PRO\' WHERE main_milestone=' . $this->id;
                        MyLink::$default->execute( $sql );

                        $sql = 'UPDATE milestones SET privacy_milestone=\'R_ENT_PRO\' WHERE id=' . $this->id;
                        MyLink::$default->execute( $sql );

                        $this->notifyOwner( 'PRIVACY' );
                    }
                } else if ( $_POST[ 'privacy' ] == 'R_PRV_PRO' ) {
                    if ( $this->privacy_milestone != 'R_PRV_PRO' ) { //visibilidad
                        $sql = 'UPDATE milestones SET privacy_milestone=\'R_PRV_PRO\' WHERE main_milestone=' . $this->id;
                        MyLink::$default->execute( $sql );

                        $sql = 'UPDATE milestones SET privacy_milestone=\'R_PRV_PRO\' WHERE id=' . $this->id;
                        MyLink::$default->execute( $sql );

                        $this->notifyOwner( 'PRIVACY' );
                    }
                }
            }

            $e->cancelBubble();
        }
        else parent::doPOST( $e );
    }

    //estos métodos se ejecutan desde el enclosed
    function attach( $enclose )
    {

        if ( $this->enclosable ) {

            $sql = 'INSERT INTO milestones_encloseds (milestone,enclosed) VALUES (' . $this->id . ',' . $enclose . ')';
            MyLink::$default->execute( $sql );

            $sql = 'UPDATE milestones SET updated=NOW(), num_encloseds=num_encloseds+1 WHERE id=' . $this->id;
            MyLink::$default->execute( $sql );
        }
    }

    //estos métodos se ejecutan desde el enclosed
    function unattach( $enclose )
    {

        if ( $this->enclosable ) {

            $sql = 'DELETE FROM milestones_encloseds WHERE milestone=' . $this->id . ' AND enclosed=' . $enclose;
            MyLink::$default->execute( $sql );

            $sql = 'UPDATE milestones SET updated=NOW(), num_encloseds=num_encloseds-1 WHERE id=' . $this->id;
            MyLink::$default->execute( $sql );
        }
    }

    function delete()
    {
        if ( $this->num_childs == 0 ) {
            //borramos

            $sql = 'DELETE FROM milestones_encloseds WHERE milestone=' . $this->id;
            MyLink::$default->execute( $sql );
            //tenemos que borrar tb todos los enclodeds
        }
    }

}

?>
